package com.ruoyi.web.controller.acitviti;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.workFlow.domain.dto.ProcessDefinitionDTO;
import com.ruoyi.workFlow.service.IProcessDefinitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName ProcessDefinitionController
 * @Description 流程定义controller
 * @Author leizhang
 * Date 2021/10/28 9:52 上午
 * @Version 1.0
 **/
@RestController
@RequestMapping("/processDefinition")
public class ProcessDefinitionController extends BaseController {

    @Autowired
    public IProcessDefinitionService processDefinitionService;

    /**
     * @param processDefinition
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @Description : 流程定义数据查询
     * @Author : leizhang
     * @Date 9:54 上午 2021/10/28
     **/
    @GetMapping(value = "/list")
    public TableDataInfo list(ProcessDefinitionDTO processDefinition) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        return getDataTable(processDefinitionService.selectProcessDefinitionList(processDefinition, pageDomain));
    }

    /**
     * @param deploymentId
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 流程定义删除
     * @Author : leizhang
     * @Date 10:04 上午 2021/10/28
     **/
    @DeleteMapping(value = "/remove/{deploymentId}")
    public AjaxResult delDefinition(@PathVariable("deploymentId") String deploymentId) {
        return toAjax(processDefinitionService.deleteProcessDefinitionById(deploymentId));
    }

    /**
     * @param file
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 上传并并部署流程
     * @Author : leizhang
     * @Date 10:06 上午 2021/10/28
     **/
    @PostMapping(value = "/uploadStreamAndDeployment")
    public AjaxResult uploadStreamAndDeployment(@RequestParam("file") MultipartFile file) throws IOException {
        processDefinitionService.uploadStreamAndDeployment(file);
        return AjaxResult.success();
    }

    /**
     * @param processDefinition
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 启动挂起流程
     * @Author : leizhang
     * @Date 10:10 上午 2021/10/28
     **/
    @PostMapping("/suspendOrActiveApply")
    @ResponseBody
    public AjaxResult suspendOrActiveApply(@RequestBody ProcessDefinitionDTO processDefinition) {
        processDefinitionService.suspendOrActiveApply(processDefinition.getId(), processDefinition.getSuspendState());
        return AjaxResult.success();
    }

    /**
     * @param multipartFile
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 上传流程
     * @Author : leizhang
     * @Date 10:14 上午 2021/10/28
     **/
    @PostMapping(value = "/upload")
    public AjaxResult upload(@RequestParam("processFile") MultipartFile multipartFile) throws IOException {
        if (!multipartFile.isEmpty()) {
            String fileName = processDefinitionService.upload(multipartFile);
            return AjaxResult.success("操作成功", fileName);
        }
        return AjaxResult.error("不允许上传空文件！");
    }

    /**
     * @param stringBPMN
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 通过stringBPMN添加流程定义
     * @Author : leizhang
     * @Date 8:49 下午 2021/10/28
     **/
    @PostMapping(value = "/addDeploymentByString")
    public AjaxResult addDeploymentByString(@RequestParam("stringBPMN") String stringBPMN) {
        processDefinitionService.addDeploymentByString(stringBPMN);
        return AjaxResult.success();
    }

    /**
     * @param response
     * @param deploymentId
     * @param resourceName
     * @return void
     * @Description : 获取流程定义XML
     * @Author : leizhang
     * @Date 9:42 下午 2021/10/28
     **/
    @GetMapping(value = "/getDefinitionXML")
    public void getProcessDefineXML(HttpServletResponse response,
                                    @RequestParam("deploymentId") String deploymentId,
                                    @RequestParam("resourceName") String resourceName) throws IOException {
        processDefinitionService.getProcessDefineXML(response, deploymentId, resourceName);
    }

    /**
     * @param instanceId
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 查看流程定义
     * @Author : leizhang
     * @Date 9:38 上午 2021/11/7
     **/
    @GetMapping(value = "/getDefinitions/{instanceId}")
    public AjaxResult getDefinitionsByInstanceId(@PathVariable("instanceId") String instanceId) {
        return AjaxResult.success(processDefinitionService.getDefinitionsByInstanceId(instanceId));
    }
}
